﻿using NLog;
using System;

namespace XUtility
{
    public class LogClass
    {
        public static Logger logger = LogManager.GetCurrentClassLogger();

        #region Debug
        public static void Debug(string msg, params object[] args)
        {
            logger.Debug(msg, args);
        }

        public static void Debug(string msg, Exception err)
        {
            logger.Debug(err, msg);
        }
        #endregion

        #region Info
        public static void Info(string msg, params object[] args)
        {
            logger.Info(msg, args);
        }

        public static void Info(string msg, Exception err)
        {
            logger.Info(err, msg);
        }
        #endregion

        #region Warn
        public static void Warn(string msg, params object[] args)
        {
            logger.Warn(msg, args);
        }

        public static void Warn(string msg, Exception err)
        {
            logger.Warn(err, msg);
        }
        #endregion

        #region Trace
        public static void Trace(string msg, params object[] args)
        {
            logger.Trace(msg, args);
        }

        public static void Trace(string msg, Exception err)
        {
            logger.Trace(err, msg);
        }
        #endregion

        #region Error
        public static void Error(string msg, params object[] args)
        {
            logger.Error(msg, args);
        }

        public static void Error(string msg, Exception err)
        {
            logger.Error(err, msg);
        }
        #endregion

        #region Fatal
        public static void Fatal(string msg, params object[] args)
        {
            logger.Fatal(msg, args);
        }

        public static void Fatal(string msg, Exception err)
        {
            logger.Fatal(err, msg);
        }
        #endregion

        #region Custom
        /// <summary>
        /// 自定义日志扩展属性,并记录到数据库
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="className"></param>
        /// <param name="MethodName"></param>
        /// <param name="IP"></param>
        public static void LogToDB(String msg,String className,String MethodName,String IP)
        {
            var ei = new MyLogEventInfo(LogLevel.Error, logger.Name, msg);
            ei.TimeStamp = DateTime.Now;
            ei.Properties["Biller"] = MethodName;
            ei.Properties["Funtion"] = className;
            ei.Properties["IP"] = IP;

            logger.Log(LogLevel.Fatal, ei);
        }

        #endregion


        /// <summary>
        /// Flush any pending log messages (in case of asynchronous targets).
        /// </summary>
        /// <param name="timeoutMilliseconds">Maximum time to allow for the flush. Any messages after that time will be discarded.</param>
        public static void Flush(int? timeoutMilliseconds = null)
        {
            if (timeoutMilliseconds != null)
                NLog.LogManager.Flush(timeoutMilliseconds.Value);

            NLog.LogManager.Flush();
        }
    }

    public class MyLogEventInfo : LogEventInfo
    {
        public MyLogEventInfo() { }
        public MyLogEventInfo(LogLevel level, string loggerName, string message) : base(level, loggerName, message)
        { }

        public override string ToString()
        {
            //Message format
            //Log Event: Logger='XXX' Level=Info Message='XXX' SequenceID=5
            return FormattedMessage;
        }
    }
}
